<?php
/**
 * 商品评论
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Goods;

use App\Admin\Actions\Goods\ContentClearTopping;
use App\Admin\Actions\Goods\ContentTopping;
use App\Admin\Actions\Goods\GoodsContentOff;
use App\Admin\Actions\Goods\GoodsContentOn;
use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\GoodsContentModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Show;


class ContentController extends Controller
{
    use ModelForm;

    protected $header = '商品评论';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    public function grid(){

        $grid = new Grid(new GoodsContentModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('User.nickname','用户昵称')->display(function($filed){
            return empty($filed) ? "微信用户" : $filed;
        });

        $grid->column('Goods.goods_title','商品名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('order_sn','订单号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('img','图片')->display(function($filed){
            return empty($filed) ? array() : json_decode($filed,true);
        })->lightbox(['width' => 30, 'height' => 30]);


        $grid->column('stars','星级')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed."星";
        });

        $grid->column('content','内容')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('created_at','评论时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        $grid->column('status','状态')->using(DataDictionaryModel::LossReportStatus, '未知')->dot([
            0 => 'danger',
            1 => 'info',
            2 => 'warning'
        ], 'success');

        $grid->column('id', '置頂')->display(function ($id, $column) use ($grid) {
            if($this->is_remord == 1){
                $action = new ContentClearTopping();
            }else{
                $action = new ContentTopping();
            }
            if(!empty($action)){
                return $action
                    ->asColumn()
                    ->setGrid($grid)
                    ->setColumn($column)
                    ->setRow($this);
            }
        });


        //搜索
        $grid->filter(function($filter){

            $filter->column(1/2, function ($filter) {
                $filter->like('User.nickname','用户昵称');
                $filter->equal('status', '状态')->select(DataDictionaryModel::LossReportStatus);
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('Goods.name','商品名称');
                $filter->between('created_at', '评论时间')->datetime();
            });
            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->disableActions();

        $grid->disableCreateButton();

        $grid->tools(function ($tools) {
            $tools->append(new GoodsContentOn());
            $tools->append(new GoodsContentOff());
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->fixColumns(1, -1);

        $grid->paginate(10);

        return $grid;
    }

}